package icu.lookyousmileface.netty.simple.mylmpl;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

import java.io.Serializable;

/**
 * @author shenj
 * @title: ClientHandler
 * @projectName NettyPro
 * @date 2020/12/4 8:37
 */
public class ClientHandler extends ChannelInboundHandlerAdapter implements Serializable {
    /**
     * @author shenj
     * @title: channelActive
     * @date 2020/12/4  8:39
     * send
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ctx.writeAndFlush(Unpooled.copiedBuffer("Hello,Sever!", CharsetUtil.UTF_8));
    }
    /**
     * @author shenj
     * @title: channelRead
     * @date 2020/12/4  8:39
     * read
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("Server's send is :"+((ByteBuf)msg).toString(CharsetUtil.UTF_8));
        System.out.println("This Server Local is :"+ctx.channel().remoteAddress());
    }
    /**
     * @author shenj
     * @title: exceptionCaught
     * @date 2020/12/4  8:39
     * try bug
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
